﻿Imports DotNetNuke
Imports System.IO
Imports System.Web.UI.WebControls
Imports System.Text.RegularExpressions
Imports System.Text
Imports System.Xml
Imports DotNetNuke.Services.Social.Notifications
Imports DotNetNuke.Services.Localization
Imports DotNetNuke.Framework
Imports DotNetNuke.Services.FileSystem
Imports DotNetNuke.Common.Utilities

Namespace DotNetNuke.Modules.ActiveForums
    Partial Public Class af_post
        Inherits ForumBase
        Protected WithEvents ctlForm As New Controls.SubmitForm
        Private isApproved As Boolean = False
        Public spinner As String
        Public EditorClientId As String
        Public PreviewText As String = ""
        Private isEdit As Boolean = False
        Dim fi As Forum
        Dim ui As New UserProfileInfo
        Dim MyThemePath As String = String.Empty
        Private sPostBack As String = String.Empty
        Private UserIsTrusted As Boolean = False
        Private _contentId As Integer = -1
        Private _authorId As Integer = -1
        Private _allowHTML As Boolean = False
        Private _editorType As EditorTypes = EditorTypes.TEXTBOX
        Dim canModEdit As Boolean = False
        Dim canModApprove As Boolean = False
        Dim canEdit As Boolean = False
        Dim canAttach As Boolean = False
        Dim canTrust As Boolean = False
        Dim canLock As Boolean = False
        Dim canPin As Boolean = False
        Dim canAnnounce As Boolean = False



#Region "Event Handlers"

        Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            ServicesFramework.Instance.RequestAjaxAntiForgerySupport()
            Dim oLink As System.Web.UI.HtmlControls.HtmlGenericControl = New System.Web.UI.HtmlControls.HtmlGenericControl("link")
            oLink.Attributes("rel") = "stylesheet"
            oLink.Attributes("type") = "text/css"
            oLink.Attributes("href") = Page.ResolveUrl("~/DesktopModules/ActiveForums/scripts/calendar.css")
            Dim oCSS As System.Web.UI.Control = Me.Page.FindControl("CSS")
            If Not oCSS Is Nothing Then
                Dim iControlIndex As Integer = 0
                iControlIndex = oCSS.Controls.Count
                oCSS.Controls.Add(oLink)
            End If

            fi = Me.ForumInfo
            _authorId = UserId
            canModEdit = Permissions.HasPerm(fi.Security.ModEdit, ForumUser.UserRoles)
            canModApprove = Permissions.HasPerm(fi.Security.ModApprove, ForumUser.UserRoles)
            canEdit = Permissions.HasPerm(fi.Security.Edit, ForumUser.UserRoles)
            'CanReply = Permissions.HasPerm(fi.Security.Reply, ForumUser.UserRoles)
            'CanCreate = Permissions.HasPerm(fi.Security.Create, ForumUser.UserRoles)
            canAttach = Permissions.HasPerm(fi.Security.Attach, ForumUser.UserRoles)
            canTrust = Permissions.HasPerm(fi.Security.Trust, ForumUser.UserRoles)
            canLock = Permissions.HasPerm(fi.Security.Lock, ForumUser.UserRoles)
            canPin = Permissions.HasPerm(fi.Security.Pin, ForumUser.UserRoles)
            canAnnounce = Permissions.HasPerm(fi.Security.Announce, ForumUser.UserRoles)
            If fi Is Nothing Then
                Response.Redirect(NavigateUrl(ForumTabId))
            ElseIf Not Request.Params("action") Is Nothing Then
                If Not canEdit And (Request.Params("action").ToLowerInvariant = "te" Or Request.Params("action").ToLowerInvariant = "re") Then
                    Response.Redirect(NavigateUrl(ForumTabId))
                End If
            End If
            If CanCreate = False And CanReply = False Then
                Response.Redirect(NavigateUrl(ForumTabId, "", "ctl=login") & "?returnurl=" & Server.UrlEncode(Request.RawUrl))
            End If

            If UserId > 0 Then
                ui = ForumUser.Profile
            Else
                ui.TopicCount = 0
                ui.ReplyCount = 0
                ui.RewardPoints = 0
                ui.IsMod = False
                ui.TrustLevel = -1

            End If
            UserIsTrusted = Utilities.IsTrusted(fi.DefaultTrustValue, ui.TrustLevel, canTrust, fi.AutoTrustLevel, ui.PostCount)
            spinner = Page.ResolveUrl("~/DesktopModules/activeforums/themes/" & MainSettings.Theme & "/images/loading.gif")
            isApproved = CBool(IIf(fi.IsModerated = True, False, True))
            If UserIsTrusted Or canModApprove Then
                isApproved = True
            End If
            Dim MyTheme As String = MainSettings.Theme
            MyThemePath = Page.ResolveUrl("~/DesktopModules/ActiveForums/themes/" & MyTheme)
            ctlForm.ID = "ctlForm"
            ctlForm.PostButton.ImageUrl = MyThemePath & "/images/save32.png"
            ctlForm.PostButton.ImageLocation = "TOP"
            ctlForm.PostButton.Height = Unit.Pixel(50)
            ctlForm.PostButton.Width = Unit.Pixel(50)
            If canAttach And fi.AllowAttach Then
                ctlForm.PostButton.ClientSideScript = "af_checkupload();"
                sPostBack = "GET" 'Page.ClientScript.GetPostBackEventReference(ctlForm.SubmitButton, String.Empty)
                ctlForm.PostButton.PostBack = False
            Else
                ctlForm.PostButton.ClientSideScript = "af_checkupload();"
                'ctlForm.PostButton.ClientSideScript = Page.ClientScript.GetPostBackEventReference(Me, String.Empty)
                ctlForm.PostButton.PostBack = False
            End If
            ctlForm.CancelButton.ImageUrl = MyThemePath & "/images/cancel32.png"
            ctlForm.CancelButton.ImageLocation = "TOP"
            ctlForm.CancelButton.PostBack = False
            ctlForm.CancelButton.ClientSideScript = "javascript:history.go(-1);"
            ctlForm.CancelButton.Confirm = True
            ctlForm.CancelButton.Height = Unit.Pixel(50)
            ctlForm.CancelButton.Width = Unit.Pixel(50)
            ctlForm.CancelButton.ConfirmMessage = GetSharedResource("[RESX:ConfirmCancel]")
            ctlForm.ModuleConfiguration = Me.ModuleConfiguration
            ctlForm.Subscribe = UserPrefTopicSubscribe
            If fi.AllowHTML Then
                _allowHTML = isHTMLPermitted(fi.EditorPermittedUsers, UserIsTrusted, canModEdit)
            End If
            ctlForm.AllowHTML = _allowHTML
            If _allowHTML Then
                _editorType = fi.EditorType
            Else

                _editorType = EditorTypes.TEXTBOX
            End If
            If Request.Browser.IsMobileDevice Then
                _editorType = EditorTypes.TEXTBOX
                _allowHTML = False
            End If
            ctlForm.EditorType = _editorType
            ctlForm.ForumInfo = fi
            ctlForm.RequireCaptcha = True
            If _editorType = EditorTypes.TEXTBOX Then
                Page.ClientScript.RegisterClientScriptInclude("afeditor", Page.ResolveUrl("~/desktopmodules/activeforums/scripts/text_editor.js"))
            ElseIf _editorType = EditorTypes.ACTIVEEDITOR Then
                Page.ClientScript.RegisterClientScriptInclude("afeditor", Page.ResolveUrl("~/desktopmodules/activeforums/scripts/active_editor.js"))
            Else
                Dim _prov As New DotNetNuke.Framework.Providers.ProviderConfiguration
                _prov = DotNetNuke.Framework.Providers.ProviderConfiguration.GetProviderConfiguration("htmlEditor")

                If _prov.DefaultProvider.ToLowerInvariant.Contains("telerik") Or _prov.DefaultProvider.ToLowerInvariant.Contains("radeditor") Then
                    Page.ClientScript.RegisterClientScriptInclude("afeditor", Page.ResolveUrl("~/desktopmodules/activeforums/scripts/telerik_editor.js"))
                ElseIf _prov.DefaultProvider.ToLowerInvariant.Contains("fck") Then
                    Page.ClientScript.RegisterClientScriptInclude("afeditor", Page.ResolveUrl("~/desktopmodules/activeforums/scripts/fck_editor.js"))
                Else
                    Page.ClientScript.RegisterClientScriptInclude("afeditor", Page.ResolveUrl("~/desktopmodules/activeforums/scripts/other_editor.js"))
                End If
            End If
            If Not Request.Params("action") Is Nothing Then
                Select Case Request.Params("action").ToLowerInvariant
                    Case "te" 'Topic Edit
                        If canModEdit Or (canEdit And Request.IsAuthenticated) Then
                            isEdit = True
                            PrepareTopic()
                            LoadTopic()
                        End If
                    Case "re" 'Reply Edit
                        If canModEdit Or (canEdit And Request.IsAuthenticated) Then
                            isEdit = True
                            PrepareReply()
                            LoadReply()
                        End If
                    Case "reply"
                        If CanReply Then
                            PrepareReply()
                        End If
                    Case "new"
                        If CanCreate Then
                            PrepareTopic()

                        End If
                    Case Else
                        If CanCreate Then
                            PrepareTopic()
                        End If
                End Select

            Else
                If QuoteId = 0 And ReplyId = 0 And TopicId = -1 And CanCreate Then
                    PrepareTopic()
                ElseIf (QuoteId > 0 Or ReplyId > 0 Or TopicId > 0) And CanReply Then
                    PrepareReply()
                End If
            End If
            If isEdit And Not Request.IsAuthenticated Then
                Response.Redirect(NavigateUrl(ForumTabId))
            End If
            ctlForm.ContentId = _contentId
            ctlForm.AuthorId = _authorId
            plhContent.Controls.Add(ctlForm)


            EditorClientId = ctlForm.ClientID
            'Page.ClientScript.RegisterClientScriptInclude("aftags", Page.ResolveUrl("~/desktopmodules/activeforums/scripts/jquery.tokeninput.js"))
        End Sub
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load






        End Sub
        Private Sub ctlForm_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ctlForm.BubbleClick
            Page.Validate()
            Dim iFloodInterval As Integer = MainSettings.FloodInterval
            If iFloodInterval > 0 Then

                If Not ForumUser Is Nothing Then
                    If DateDiff(DateInterval.Second, ForumUser.Profile.DateLastPost, Now()) < iFloodInterval Then
                        Dim im As New Controls.InfoMessage
                        im.Message = "<div class=""afmessage"">" & String.Format(GetSharedResource("[RESX:Error:FloodControl]"), iFloodInterval) & "</div>"
                        plhMessage.Controls.Add(im)
                        Exit Sub
                    End If
                End If
            End If
            If Page.IsValid And Utilities.InputIsValid(ctlForm.Body.Trim) And Utilities.InputIsValid(ctlForm.Subject) Then
                If TopicId = -1 Or (TopicId > 0 And Request.Params("action") = "te") Then
                    If ValidateProperties() Then
                        SaveTopic()
                    End If

                Else
                    SaveReply()
                End If
            End If

        End Sub
        Private Function ValidateProperties() As Boolean
            If Not ForumInfo.Properties Is Nothing AndAlso ForumInfo.Properties.Count > 0 Then
                For Each p As PropertiesInfo In ForumInfo.Properties
                    If p.IsRequired Then
                        If Request.Form("afprop-" & p.PropertyId) Is Nothing Then
                            Return False
                        ElseIf Not Request.Form("afprop-" & p.PropertyId) Is Nothing AndAlso String.IsNullOrEmpty(Request.Form("afprop-" & p.PropertyId).ToString.Trim) Then
                            Return False
                        End If
                    End If
                    If Not String.IsNullOrEmpty(p.ValidationExpression) AndAlso Not String.IsNullOrEmpty(Request.Form("afprop-" & p.PropertyId).ToString.Trim) Then
                        Dim isMatch As Boolean = Regex.IsMatch(Request.Form("afprop-" & p.PropertyId).ToString.Trim, p.ValidationExpression, RegexOptions.IgnoreCase)
                        If Not isMatch Then
                            Return False
                        End If
                    End If
                Next
                Return True
            Else
                Return True
            End If
        End Function
        Private Sub cbPreview_Callback(ByVal sender As Object, ByVal e As Modules.ActiveForums.Controls.CallBackEventArgs) Handles cbPreview.Callback
            Select Case e.Parameters(0).ToLower
                Case "preview"
                    Dim message As String = e.Parameters(1).ToString
                    Dim TopicTemplateID As Integer = 0

                    TopicTemplateID = ForumInfo.TopicTemplateId
                    message = Utilities.CleanString(PortalId, message, _allowHTML, _editorType, ForumInfo.UseFilter, ForumInfo.AllowScript, ForumModuleId, ImagePath, ForumInfo.AllowEmoticons)
                    message = Utilities.ManageImagePath(message)
                    Dim uc As New UserController
                    Dim up As User = uc.GetUser(PortalId, ForumModuleId, UserId)
                    If up Is Nothing Then
                        up = New User
                        up.UserId = -1
                        up.UserName = "guest"
                        up.Profile.TopicCount = 0
                        up.Profile.ReplyCount = 0
                        up.DateCreated = Now()

                    End If
                    message = TemplateUtils.PreviewTopic(TopicTemplateID, PortalId, ForumModuleId, ForumTabId, ForumInfo, UserId, message, ImagePath, up, Now(), CurrentUserType, TimeZoneOffset)
                    hidPreviewText.Value = message

            End Select
            hidPreviewText.RenderControl(e.Output)
        End Sub
#End Region
#Region "Private Methods"
        Private Sub LoadTopic()
            ctlForm.EditorMode = Modules.ActiveForums.Controls.SubmitForm.EditorModes.EditTopic
            Dim ti As TopicInfo
            Dim tc As New TopicsController
            ti = tc.Topics_Get(PortalId, ForumModuleId, TopicId, ForumId, UserId, True)
            If ti Is Nothing Then
                Response.Redirect(NavigateUrl(ForumTabId))
            ElseIf (ti.Content.AuthorId <> Me.UserId And canModEdit = False) Or (ti.Content.AuthorId = Me.UserId And canEdit = False) Or (canEdit = False And canModEdit) Then
                Response.Redirect(NavigateUrl(ForumTabId))
            ElseIf Not canModEdit And (ti.Content.AuthorId = Me.UserId And canEdit And MainSettings.EditInterval > 0 And DateDiff(DateInterval.Minute, ti.Content.DateCreated, Now()) > MainSettings.EditInterval) Then
                Dim im As New Controls.InfoMessage
                im.Message = "<div class=""afmessage"">" & String.Format(GetSharedResource("[RESX:Message:EditIntervalReached]"), MainSettings.EditInterval.ToString) & "</div>"
                plhMessage.Controls.Add(im)
                plhContent.Controls.Clear()
            Else
                'User has acccess
                Dim sBody As String = ti.Content.Body
                Dim sSubject As String = ti.Content.Subject
                sBody = Utilities.PrepareForEdit(PortalId, ForumModuleId, ImagePath, sBody, _allowHTML, _editorType)
                sSubject = Utilities.PrepareForEdit(PortalId, ForumModuleId, ImagePath, sSubject, False, EditorTypes.TEXTBOX)
                With ctlForm
                    .Subject = sSubject
                    .Summary = ti.Content.Summary
                    .Body = sBody
                    .AnnounceEnd = ti.AnnounceEnd
                    .AnnounceStart = ti.AnnounceStart
                    .Locked = ti.IsLocked
                    .Pinned = ti.IsPinned
                    .TopicIcon = ti.TopicIcon
                    .Tags = ti.Tags
                    .Categories = ti.Categories
                    .IsApproved = ti.IsApproved
                    .StatusId = ti.StatusId
                    .TopicPriority = ti.Priority
                    If ti.Author.AuthorId > 0 Then
                        .Subscribe = Subscriptions.IsSubscribed(PortalId, ForumModuleId, ForumId, TopicId, SubscriptionTypes.Instant, ti.Author.AuthorId)
                    End If
                    _contentId = ti.ContentId
                    _authorId = ti.Author.AuthorId
                End With
                If Not String.IsNullOrEmpty(ti.TopicData) Then
                    Dim pl As New List(Of PropertiesInfo)
                    Dim xDoc As New XmlDocument
                    xDoc.LoadXml(ti.TopicData)
                    If Not xDoc Is Nothing Then
                        Dim xRoot As System.Xml.XmlNode = xDoc.DocumentElement
                        Dim xNodeList As System.Xml.XmlNodeList = xRoot.SelectNodes("//properties/property")
                        If xNodeList.Count > 0 Then
                            Dim i As Integer = 0
                            For i = 0 To xNodeList.Count - 1
                                Dim pName As String = Utilities.HTMLDecode(xNodeList(i).ChildNodes.Item(0).InnerText)
                                Dim pValue As String = Utilities.HTMLDecode(xNodeList(i).ChildNodes.Item(1).InnerText)
                                Dim pId As Integer = CInt(xNodeList(i).Attributes("id").Value)
                                Dim p As New PropertiesInfo
                                p.Name = pName
                                p.DefaultValue = pValue
                                p.PropertyId = pId
                                pl.Add(p)
                            Next
                        End If
                    End If
                    ctlForm.TopicProperties = pl
                End If
                If ti.TopicType = TopicTypes.Poll Then
                    'Get Poll
                    Dim ds As DataSet = DataProvider.Instance.Poll_Get(ti.TopicId)
                    If ds.Tables.Count > 0 Then
                        Dim pollRow As DataRow = ds.Tables(0).Rows(0)
                        ctlForm.PollQuestion = pollRow("Question").ToString
                        ctlForm.PollType = pollRow("PollType").ToString

                        For Each dr As DataRow In ds.Tables(1).Rows
                            ctlForm.PollOptions &= dr("OptionName").ToString & vbCrLf
                        Next
                    End If
                End If
                If Not ti.Content.AuthorId = Me.UserId And canModApprove Then
                    ctlForm.ShowModOptions = True
                End If
            End If
        End Sub
        Private Sub LoadReply()
            'Edit a Reply
            ctlForm.EditorMode = Modules.ActiveForums.Controls.SubmitForm.EditorModes.EditReply
            Dim ri As ReplyInfo
            Dim rc As New ReplyController
            ri = rc.Reply_Get(PortalId, ForumModuleId, TopicId, PostId)
            If ri Is Nothing Then
                'Load Reply Not Found
                'Dim im As New Controls.InfoMessage
                'im.Message = "<div class=""afmessage"">" & GetSharedResource("[RESX:Message:TopicNotFound]") & "</div>"
                'plhMessage.Controls.Add(im)
                'plhContent.Controls.Clear()
                Response.Redirect(NavigateUrl(ForumTabId))
            ElseIf (ri.Content.AuthorId <> Me.UserId And canModEdit = False) Or (ri.Content.AuthorId = Me.UserId And canEdit = False) Or (canEdit = False And canModEdit) Then
                'Load Access Denied
                'Dim im As New Controls.InfoMessage
                'im.Message = "<div class=""afmessage"">" & GetSharedResource("[RESX:Message:AccessDenied]") & "</div>"
                'plhMessage.Controls.Add(im)
                'plhContent.Controls.Clear()
                Response.Redirect(NavigateUrl(ForumTabId))
            ElseIf Not canModEdit And (ri.Content.AuthorId = Me.UserId And canEdit And MainSettings.EditInterval > 0 And DateDiff(DateInterval.Minute, ri.Content.DateCreated, Now()) > MainSettings.EditInterval) Then
                Dim im As New Controls.InfoMessage
                im.Message = "<div class=""afmessage"">" & String.Format(GetSharedResource("[RESX:Message:EditIntervalReached]"), MainSettings.EditInterval.ToString) & "</div>"
                plhMessage.Controls.Add(im)
                plhContent.Controls.Clear()
            Else
                Dim sBody As String = ri.Content.Body
                Dim sSubject As String = ri.Content.Subject
                sBody = Utilities.PrepareForEdit(PortalId, ForumModuleId, ImagePath, sBody, _allowHTML, _editorType)
                sSubject = Utilities.PrepareForEdit(PortalId, ForumModuleId, ImagePath, sSubject, False, EditorTypes.TEXTBOX)
                ctlForm.Subject = sSubject
                ctlForm.Body = sBody
                ctlForm.IsApproved = ri.IsApproved
                _contentId = ri.ContentId
                _authorId = ri.Author.AuthorId
                If ri.Author.AuthorId > 0 Then
                    ctlForm.Subscribe = Subscriptions.IsSubscribed(PortalId, ForumModuleId, ForumId, TopicId, SubscriptionTypes.Instant, ri.Author.AuthorId)
                End If
                If Not ri.Content.AuthorId = Me.UserId And canModApprove Then
                    ctlForm.ShowModOptions = True
                End If
            End If

        End Sub
        Private Sub PrepareTopic()
            Dim Template As String = String.Empty
            If fi.TopicFormId = 0 Then
                Dim myFile As String = String.Empty
                myFile = Request.MapPath(DotNetNuke.Common.Globals.ApplicationPath) & "\DesktopModules\ActiveForums\config\templates\TopicEditor.txt"
                Template = File.ReadAllText(myFile)
            Else
                Dim tc As New TemplateController
                Dim ti As TemplateInfo = tc.Template_Get(fi.TopicFormId, PortalId, ForumModuleId)
                Template = ti.TemplateHTML
            End If

            If MainSettings.UseSkinBreadCrumb Then
                Dim sCrumb As String = "<a href=""" & NavigateUrl(TabId, "", ParamKeys.GroupId & "=" & ForumInfo.ForumGroupId.ToString) & """>" & ForumInfo.GroupName & "</a>|"
                sCrumb &= "<a href=""" & NavigateUrl(TabId, "", ParamKeys.ForumId & "=" & ForumInfo.ForumID.ToString) & """>" & ForumInfo.ForumName & "</a>"
                If Environment.UpdateBreadCrumb(Page.Controls, sCrumb) Then
                    Template = Template.Replace("<div class=""afcrumb"">[AF:LINK:FORUMMAIN] > [AF:LINK:FORUMGROUP] > [AF:LINK:FORUMNAME]</div>", String.Empty)
                End If
            End If


            ctlForm.EditorMode = Modules.ActiveForums.Controls.SubmitForm.EditorModes.NewTopic

            If Permissions.HasPerm(fi.Security.ModApprove, ForumUser.UserRoles) Then
                ctlForm.ShowModOptions = True
            End If
            ctlForm.Template = Template
            ctlForm.IsApproved = isApproved

        End Sub
        ''' <summary>
        ''' Prepares the post form for creating a reply.
        ''' </summary>
        Private Sub PrepareReply()
            ctlForm.EditorMode = Modules.ActiveForums.Controls.SubmitForm.EditorModes.Reply

            Dim Template As String = String.Empty
            If fi.ReplyFormId = 0 Then
                Dim myFile As String = String.Empty
                myFile = Request.MapPath(DotNetNuke.Common.Globals.ApplicationPath) & "\DesktopModules\ActiveForums\config\templates\ReplyEditor.txt"
                Template = File.ReadAllText(myFile)
            Else
                Dim tc As New TemplateController
                Dim ti As TemplateInfo = tc.Template_Get(fi.ReplyFormId, PortalId, ForumModuleId)
                Template = ti.TemplateHTML
            End If
            If MainSettings.UseSkinBreadCrumb Then
                Template = Template.Replace("<div class=""afcrumb"">[AF:LINK:FORUMMAIN] > [AF:LINK:FORUMGROUP] > [AF:LINK:FORUMNAME]</div>", String.Empty)

            End If
            ctlForm.Template = Template
            If Not TopicId > 0 Then
                'Can't Find Topic
                Dim im As New Controls.InfoMessage
                im.Message = GetSharedResource("[RESX:Message:LoadTopicFailed]")
                plhContent.Controls.Add(im)
            ElseIf Not CanReply Then
                'No permission to reply
                Dim im As New Controls.InfoMessage
                im.Message = GetSharedResource("[RESX:Message:AccessDenied]")
                plhContent.Controls.Add(im)
            Else
                Dim ti As TopicInfo
                Dim tc As New TopicsController
                ti = tc.Topics_Get(PortalId, ForumModuleId, TopicId, ForumId, UserId, True)
                ctlForm.Subject = Utilities.GetSharedResource("[RESX:SubjectPrefix]") & " " & ti.Content.Subject
                ctlForm.TopicSubject = ti.Content.Subject
                Dim body As String = String.Empty
                If ti.IsLocked And (CurrentUserType = CurrentUserTypes.Anon Or CurrentUserType = CurrentUserTypes.Auth) Then
                    Response.Redirect(NavigateUrl(ForumTabId))
                End If
                If Not Request.Params(ParamKeys.QuoteId) Is Nothing Or Not Request.Params(ParamKeys.ReplyId) Is Nothing Or Not Request.Params(ParamKeys.PostId) Is Nothing Then
                    'Setup form for Quote or Reply with body display
                    Dim IsQuote As Boolean = False
                    Dim PostId As Integer = 0
                    Dim sPostedBy As String = Utilities.GetSharedResource("[RESX:PostedBy]") & " {0} {1} {2}"
                    If Not Request.Params(ParamKeys.QuoteId) Is Nothing Then
                        IsQuote = True
                        If IsNumeric(Request.Params(ParamKeys.QuoteId)) Then
                            PostId = CInt(Request.Params(ParamKeys.QuoteId))
                        End If
                    ElseIf Not Request.Params(ParamKeys.ReplyId) Is Nothing Then
                        If IsNumeric(Request.Params(ParamKeys.ReplyId)) Then
                            PostId = CInt(Request.Params(ParamKeys.ReplyId))
                        End If
                    ElseIf Not Request.Params(ParamKeys.PostId) Is Nothing Then
                        If IsNumeric(Request.Params(ParamKeys.PostId)) Then
                            PostId = CInt(Request.Params(ParamKeys.PostId))
                        End If

                    End If
                    If Not PostId = 0 Then
                        Dim ci As Content
                        If PostId = TopicId Then
                            ti = tc.Topics_Get(PortalId, ForumModuleId, TopicId)
                            ci = ti.Content
                            sPostedBy = String.Format(sPostedBy, UserProfiles.GetDisplayName(ForumModuleId, MainSettings.MemberListMode, False, ti.Content.AuthorId, MainSettings.UserNameDisplay, ti.Author), Utilities.GetSharedResource("On.Text"), GetServerDateTime(ti.Content.DateCreated))
                        Else
                            Dim ri As ReplyInfo
                            Dim rc As New ReplyController
                            ri = rc.Reply_Get(PortalId, ForumModuleId, TopicId, PostId)
                            ci = ri.Content
                            sPostedBy = String.Format(sPostedBy, UserProfiles.GetDisplayName(ForumModuleId, MainSettings.MemberListMode, False, ri.Content.AuthorId, MainSettings.UserNameDisplay, ri.Author), Utilities.GetSharedResource("On.Text"), GetServerDateTime(ri.Content.DateCreated))
                        End If
                        If Not ci Is Nothing Then
                            body = ci.Body
                        End If
                    End If
                    If _allowHTML And _editorType <> EditorTypes.TEXTBOX Then
                        If body.ToUpper.Contains("<CODE") Or body.ToUpper.Contains("[CODE]") Then
                            Dim objCode As New CodeParser
                            body = CodeParser.ParseCode(Utilities.HTMLDecode(body))
                        End If
                    Else
                        body = Utilities.PrepareForEdit(PortalId, ForumModuleId, ImagePath, body, _allowHTML, _editorType)
                    End If
                    If IsQuote Then
                        ctlForm.EditorMode = Modules.ActiveForums.Controls.SubmitForm.EditorModes.Quote
                        If _allowHTML And _editorType <> EditorTypes.TEXTBOX Then
                            body = "<blockquote>" & vbCrLf & sPostedBy & vbCrLf & "<br />" & vbCrLf & body & vbCrLf & "</blockquote><br /><br />"
                        Else
                            body = "[quote]" & vbCrLf & sPostedBy & vbCrLf & body & vbCrLf & "[/quote]" & vbCrLf
                        End If
                    Else
                        ctlForm.EditorMode = Modules.ActiveForums.Controls.SubmitForm.EditorModes.ReplyWithBody
                        body = sPostedBy & "<br />" & body
                    End If
                    ctlForm.Body = body


                End If
            End If
            If Not ctlForm.EditorMode = Modules.ActiveForums.Controls.SubmitForm.EditorModes.EditReply And canModApprove Then
                ctlForm.ShowModOptions = False
            End If
        End Sub
        Private Sub SaveTopic()
            Dim bSend As Boolean = True

            Dim Subject As String = String.Empty
            Dim Body As String = String.Empty
            Dim Summary As String = String.Empty
            Subject = ctlForm.Subject
            Body = ctlForm.Body
            Subject = Utilities.CleanString(PortalId, Subject, False, EditorTypes.TEXTBOX, ForumInfo.UseFilter, False, ForumModuleId, MyThemePath, False)
            Body = Utilities.CleanString(PortalId, Body, _allowHTML, _editorType, ForumInfo.UseFilter, ForumInfo.AllowScript, ForumModuleId, MyThemePath, ForumInfo.AllowEmoticons)
            Summary = ctlForm.Summary
            Dim AuthorId As Integer = -1
            Dim AuthorName As String = String.Empty
            If Request.IsAuthenticated Then
                AuthorId = UserInfo.UserID
                Select Case MainSettings.UserNameDisplay.ToUpperInvariant
                    Case "USERNAME"
                        AuthorName = Trim(UserInfo.Username)
                    Case "FULLNAME"
                        AuthorName = Trim(UserInfo.FirstName & " " & UserInfo.LastName)
                    Case "FIRSTNAME"
                        AuthorName = Trim(UserInfo.FirstName)
                    Case "LASTNAME"
                        AuthorName = Trim(UserInfo.LastName)
                    Case "DISPLAYNAME"
                        AuthorName = Trim(UserInfo.DisplayName)
                    Case Else
                        AuthorName = UserInfo.DisplayName
                End Select
                'AuthorName = UserInfo.DisplayName
            Else
                AuthorId = -1
                AuthorName = Utilities.CleanString(PortalId, ctlForm.AuthorName, False, EditorTypes.TEXTBOX, True, False, ForumModuleId, MyThemePath, False)
                If AuthorName.Trim = String.Empty Then
                    Exit Sub
                End If
            End If
            Dim tc As New TopicsController
            Dim ti As TopicInfo = Nothing
            If TopicId > 0 Then
                ti = tc.Topics_Get(PortalId, ForumModuleId, TopicId)
                ti.Content.DateUpdated = Now()
                AuthorId = ti.Author.AuthorId
                bSend = False
            Else
                ti = New TopicInfo
                Dim dt As Date = Now()
                ti.Content.DateCreated = dt
                ti.Content.DateUpdated = dt
            End If
            With ti
                .AnnounceEnd = ctlForm.AnnounceEnd
                .AnnounceStart = ctlForm.AnnounceStart
                .Priority = ctlForm.TopicPriority
                If Not isEdit Then
                    .Content.AuthorId = AuthorId
                    .Content.AuthorName = AuthorName
                    .Content.IPAddress = Request.UserHostAddress
                End If
                If Regex.IsMatch(Body, "<CODE([^>]*)>", RegexOptions.IgnoreCase) Then
                    For Each m As Match In Regex.Matches(Body, "<CODE([^>]*)>(.*?)</CODE>", RegexOptions.IgnoreCase)
                        Body = Body.Replace(m.Value, m.Value.Replace("<br>", vbCrLf))
                    Next
                End If
                If Not String.IsNullOrEmpty(ForumInfo.PrefixURL) Then
                    Dim cleanSubject As String = Utilities.CleanName(Subject).ToLowerInvariant
                    If IsNumeric(cleanSubject) Then
                        cleanSubject = "Topic-" & cleanSubject
                    End If
                    Dim topicUrl As String = cleanSubject
                    Dim urlPrefix As String = "/"
                    If Not String.IsNullOrEmpty(ForumInfo.ForumGroup.PrefixURL) Then
                        urlPrefix &= ForumInfo.ForumGroup.PrefixURL & "/"
                    End If
                    If Not String.IsNullOrEmpty(ForumInfo.PrefixURL) Then
                        urlPrefix &= ForumInfo.PrefixURL & "/"
                    End If
                    Dim urlToCheck As String = urlPrefix & cleanSubject
                    Dim topicsDb As New Data.Topics
                    For u As Integer = 0 To 200
                        Dim tid As Integer = topicsDb.TopicIdByUrl(PortalId, ModuleId, urlToCheck)
                        If tid > 0 And tid = TopicId Then
                            Exit For
                        ElseIf tid > 0 Then
                            topicUrl = (u + 1) & "-" & cleanSubject
                            urlToCheck = urlPrefix & topicUrl
                        Else
                            Exit For
                        End If
                    Next
                    If topicUrl.Length > 150 Then
                        topicUrl = topicUrl.Substring(0, 149)
                        topicUrl = topicUrl.Substring(0, topicUrl.LastIndexOf("-"))
                    End If
                    .TopicUrl = topicUrl
                    '.URL = topicUrl
                Else
                    '.URL = String.Empty
                    .TopicUrl = String.Empty
                End If

                .Content.Body = Body 'Utilities.CleanString(PortalId, Body, fi.AllowHTML, fi.EditorType, fi.UseFilter, fi.AllowScript,  ForumModuleId, String.Empty)
                .Content.Subject = Subject
                .Content.Summary = Summary
                .IsAnnounce = CBool(IIf(.AnnounceEnd <> Utilities.NullDate And .AnnounceStart <> Utilities.NullDate, True, False))
                If canModApprove And fi.IsModerated Then
                    .IsApproved = ctlForm.IsApproved
                Else
                    .IsApproved = isApproved
                End If
                bSend = .IsApproved
                .IsArchived = False
                .IsDeleted = False
                If canLock Then
                    .IsLocked = ctlForm.Locked
                Else
                    .IsLocked = False
                End If
                If canPin Then
                    .IsPinned = ctlForm.Pinned
                Else
                    .IsPinned = False
                End If
                .StatusId = ctlForm.StatusId
                .TopicIcon = ctlForm.TopicIcon
                .TopicType = 0
            End With
            If Not ForumInfo.Properties Is Nothing Then
                Dim tData As New StringBuilder
                tData.Append("<topicdata>")
                tData.Append("<properties>")
                For Each p As PropertiesInfo In ForumInfo.Properties
                    Dim pkey As String = "afprop-" & p.PropertyId.ToString

                    tData.Append("<property id=""" & p.PropertyId.ToString & """>")
                    tData.Append("<name><![CDATA[")
                    tData.Append(p.Name)
                    tData.Append("]]></name>")
                    If Not Request.Form(pkey) Is Nothing Then
                        tData.Append("<value><![CDATA[")
                        tData.Append(Utilities.XSSFilter(Request.Form(pkey)))
                        tData.Append("]]></value>")
                    Else
                        tData.Append("<value></value>")
                    End If
                    tData.Append("</property>")
                Next
                tData.Append("</properties>")
                tData.Append("</topicdata>")
                ti.TopicData = tData.ToString
            End If

            TopicId = tc.TopicSave(PortalId, ti)
            ti = tc.Topics_Get(PortalId, ForumModuleId, TopicId, ForumId, -1, False)
            If Not ti Is Nothing Then
                tc.Topics_SaveToForum(ForumId, TopicId, PortalId, ModuleId)
                SaveAttach(ti.ContentId)
                If ti.IsApproved And ti.Author.AuthorId > 0 Then
                    Dim uc As New Data.Profiles
                    uc.Profile_UpdateTopicCount(PortalId, ti.Author.AuthorId)
                End If
            End If
            If Permissions.HasPerm(ForumInfo.Security.Tag, ForumUser.UserRoles) Then
                DataProvider.Instance.Tags_DeleteByTopicId(PortalId, ForumModuleId, TopicId)
                Dim tagForm As String = String.Empty
                If Not Request.Form("txtTags") Is Nothing Then
                    tagForm = Request.Form("txtTags")
                End If
                If Not tagForm = String.Empty Then
                    Dim Tags() As String = tagForm.Split(CChar(","))
                    Dim tag As String = String.Empty
                    For Each tag In Tags
                        Dim sTag As String = Utilities.CleanString(PortalId, tag.Trim, False, EditorTypes.TEXTBOX, False, False, ForumModuleId, String.Empty, False)
                        DataProvider.Instance.Tags_Save(PortalId, ForumModuleId, -1, sTag, 0, 1, 0, TopicId, False, -1, -1)
                    Next
                End If
            End If
            If Permissions.HasPerm(ForumInfo.Security.Categorize, ForumUser.UserRoles) Then
                If Not Request.Form("amaf-catselect") Is Nothing Then
                    Dim cats() As String = Request.Form("amaf-catselect").Split(CChar(";"))
                    DataProvider.Instance.Tags_DeleteTopicToCategory(PortalId, ForumModuleId, -1, TopicId)
                    For Each c As String In cats
                        Dim cid As Integer = -1
                        If Not String.IsNullOrEmpty(c) AndAlso IsNumeric(c) Then
                            cid = CInt(c)
                            If cid > 0 Then
                                DataProvider.Instance.Tags_AddTopicToCategory(PortalId, ForumModuleId, cid, TopicId)
                            End If
                        End If
                    Next
                End If
            End If

            If Not ctlForm.PollQuestion = String.Empty And Not ctlForm.PollOptions = String.Empty Then
                Dim sPollQ As String = ctlForm.PollQuestion.Trim
                sPollQ = Utilities.CleanString(PortalId, sPollQ, False, EditorTypes.TEXTBOX, True, False, ForumModuleId, String.Empty, False)
                Dim PollId As Integer = 0
                PollId = DataProvider.Instance.Poll_Save(-1, TopicId, UserId, ctlForm.PollQuestion.Trim, ctlForm.PollType)
                If PollId > 0 Then
                    Dim Options() As String = ctlForm.PollOptions.Split(CChar(vbCrLf))
                    Dim opt As String = ""
                    For Each opt In Options
                        If opt.Trim <> "" Then
                            opt = Utilities.CleanString(PortalId, opt, False, EditorTypes.TEXTBOX, True, False, ForumModuleId, String.Empty, False)
                            DataProvider.Instance.Poll_Option_Save(-1, PollId, opt.Trim, TopicId)
                        End If

                    Next
                End If
                ti = tc.Topics_Get(PortalId, ForumModuleId, TopicId, ForumId, -1, False)
                ti.TopicType = TopicTypes.Poll
                tc.TopicSave(PortalId, ti)
            End If
            Try
                Dim cachekey As String = String.Format("AF-FV-{0}-{1}", PortalId, ModuleId)
                DataCache.CacheClearPrefix(cachekey)
                If ctlForm.Subscribe And AuthorId = UserId Then
                    If Not Subscriptions.IsSubscribed(PortalId, ForumModuleId, ForumId, TopicId, SubscriptionTypes.Instant, AuthorId) Then
                        Dim sc As New SubscriptionController
                        sc.Subscription_Update(PortalId, ForumModuleId, ForumId, TopicId, 1, AuthorId, ForumUser.UserRoles)
                    End If
                ElseIf isEdit Then
                    Dim isSub As Boolean = Subscriptions.IsSubscribed(PortalId, ForumModuleId, ForumId, TopicId, SubscriptionTypes.Instant, AuthorId)
                    If isSub And Not ctlForm.Subscribe Then
                        Dim sc As New SubscriptionController
                        sc.Subscription_Update(PortalId, ForumModuleId, ForumId, TopicId, 1, AuthorId, ForumUser.UserRoles)
                    End If
                End If
                If bSend And Not isEdit Then
                    Subscriptions.SendSubscriptions(PortalId, ForumModuleId, ForumTabId, fi, TopicId, 0, ti.Content.AuthorId)
                End If
                If ti.IsApproved = False Then
                    Dim mods As List(Of Entities.Users.UserInfo) = Utilities.GetListOfModerators(PortalId, ForumId)
                    Dim notificationType As NotificationType = NotificationsController.Instance.GetNotificationType("AF-ForumModeration")
                    Dim notifySubject As String = Utilities.GetSharedResource("NotificationSubjectTopic")
                    notifySubject = notifySubject.Replace("[DisplayName]", UserInfo.DisplayName)
                    notifySubject = notifySubject.Replace("[TopicSubject]", ti.Content.Subject)
                    Dim notifyBody As String = Utilities.GetSharedResource("NotificationBodyTopic")
                    notifyBody = notifyBody.Replace("[Post]", ti.Content.Body)
                    Dim notificationKey As String = String.Format("{0}:{1}:{2}:{3}:{4}", TabId, ForumModuleId, ForumId, TopicId, ReplyId)

                    Dim notification As New Notification
                    notification.NotificationTypeID = notificationType.NotificationTypeId
                    notification.Subject = notifySubject
                    notification.Body = notifyBody
                    notification.IncludeDismissAction = False
                    notification.SenderUserID = UserInfo.UserID
                    notification.Context = notificationKey


                    NotificationsController.Instance.SendNotification(notification, PortalId, Nothing, mods)



                    Dim Params() As String = {ParamKeys.ForumId & "=" & ForumId, ParamKeys.ViewType & "=confirmaction", ParamKeys.ConfirmActionId & "=" & ConfirmActions.MessagePending}
                    Response.Redirect(NavigateUrl(ForumTabId, "", Params), False)
                Else
                    If Not ti Is Nothing Then
                        ti.TopicId = TopicId
                    End If
                    Dim ctlUtils As New ControlUtils

                    Dim sUrl As String = ctlUtils.BuildUrl(ForumTabId, ForumModuleId, ForumInfo.ForumGroup.PrefixURL, ForumInfo.PrefixURL, ForumInfo.ForumGroupId, ForumInfo.ForumID, TopicId, ti.TopicUrl, -1, -1, String.Empty, 1, SocialGroupId)
                    If sUrl.Contains("~/") Or Not Request.QueryString("asg") Is Nothing Then
                        sUrl = Utilities.NavigateUrl(ForumTabId, "", ParamKeys.TopicId & "=" & TopicId)
                    End If

                    If Not isEdit Then
                        Try
                            Dim amas As New Social
                            amas.AddTopicToJournal(PortalId, ForumModuleId, ForumId, TopicId, UserId, sUrl, Subject, Summary, Body, ForumInfo.ActiveSocialSecurityOption, ForumInfo.Security.Read, SocialGroupId)
                            If Request.QueryString("asg") Is Nothing And Not String.IsNullOrEmpty(MainSettings.ActiveSocialTopicsKey) And ForumInfo.ActiveSocialEnabled Then
                                ' amas.AddTopicToJournal(PortalId, ForumModuleId, ForumId, UserId, sUrl, Subject, Summary, Body, ForumInfo.ActiveSocialSecurityOption, ForumInfo.Security.Read)
                            Else
                                amas.AddForumItemToJournal(PortalId, ForumModuleId, UserId, "forumtopic", sUrl, Subject, Body)
                            End If


                        Catch ex As Exception
                            DotNetNuke.Services.Exceptions.LogException(ex)
                        End Try
                    End If



                    Response.Redirect(sUrl, False)
                End If
            Catch ex As Exception
                DotNetNuke.Services.Exceptions.LogException(ex)
            End Try


        End Sub
        Private Sub SaveReply()
            Dim Subject As String = String.Empty
            Dim Body As String = String.Empty
            Dim bSend As Boolean = True
            Subject = ctlForm.Subject
            Body = ctlForm.Body
            Subject = Utilities.CleanString(PortalId, Subject, False, EditorTypes.TEXTBOX, fi.UseFilter, False, ForumModuleId, MyThemePath, False)
            Body = Utilities.CleanString(PortalId, Body, _allowHTML, _editorType, fi.UseFilter, fi.AllowScript, ForumModuleId, MyThemePath, fi.AllowEmoticons)
            Dim AuthorId As Integer = -1
            Dim AuthorName As String = String.Empty
            If Request.IsAuthenticated Then
                AuthorId = UserInfo.UserID
                Select Case MainSettings.UserNameDisplay.ToUpperInvariant
                    Case "USERNAME"
                        AuthorName = Trim(UserInfo.Username)
                    Case "FULLNAME"
                        AuthorName = Trim(UserInfo.FirstName & " " & UserInfo.LastName)
                    Case "FIRSTNAME"
                        AuthorName = Trim(UserInfo.FirstName)
                    Case "LASTNAME"
                        AuthorName = Trim(UserInfo.LastName)
                    Case "DISPLAYNAME"
                        AuthorName = Trim(UserInfo.DisplayName)
                    Case Else
                        AuthorName = UserInfo.DisplayName
                End Select
            Else
                AuthorId = -1
                AuthorName = Utilities.CleanString(PortalId, ctlForm.AuthorName, False, EditorTypes.TEXTBOX, True, False, ForumModuleId, MyThemePath, False)
                If AuthorName.Trim = String.Empty Then
                    Exit Sub
                End If
            End If

            Dim tc As New TopicsController
            Dim rc As New ReplyController
            Dim ri As ReplyInfo = Nothing
            If PostId > 0 Then
                ri = rc.Reply_Get(PortalId, ForumModuleId, TopicId, PostId)
                ri.Content.DateUpdated = Now()
                bSend = False
            Else
                ri = New ReplyInfo
                Dim dt As Date = Now()
                ri.Content.DateCreated = dt
                ri.Content.DateUpdated = dt
                'AuthorId = ri.Author.AuthorId
            End If
            With ri
                If Not isEdit Then
                    .Content.AuthorId = AuthorId
                    .Content.AuthorName = AuthorName
                    .Content.IPAddress = Request.UserHostAddress
                End If
                If Regex.IsMatch(Body, "<CODE([^>]*)>", RegexOptions.IgnoreCase) Then
                    For Each m As Match In Regex.Matches(Body, "<CODE([^>]*)>(.*?)</CODE>", RegexOptions.IgnoreCase)
                        Body = Body.Replace(m.Value, m.Value.Replace("<br>", vbCrLf))
                    Next


                End If
                .Content.Body = Body
                .Content.Subject = Subject
                .Content.Summary = String.Empty
                If Not ctlForm.IsApproved = Nothing And canModApprove And .Content.AuthorId <> Me.UserId Then
                    .IsApproved = ctlForm.IsApproved
                Else
                    .IsApproved = isApproved
                End If
                bSend = .IsApproved
                .IsDeleted = False
                .StatusId = ctlForm.StatusId
                .TopicId = TopicId
            End With
            Dim tmpReplyId As Integer = -1
            tmpReplyId = rc.Reply_Save(PortalId, ri)
            ri = rc.Reply_Get(PortalId, ForumModuleId, TopicId, tmpReplyId)
            SaveAttach(ri.ContentId)
            'tc.ForumTopicSave(ForumID, TopicId, ReplyId)
            Dim cachekey As String = String.Format("AF-FV-{0}-{1}", PortalId, ModuleId)
            DataCache.CacheClearPrefix(cachekey)
            Try
                If ctlForm.Subscribe And AuthorId = UserId Then
                    If Not Subscriptions.IsSubscribed(PortalId, ForumModuleId, ForumId, TopicId, SubscriptionTypes.Instant, AuthorId) Then
                        Dim sc As New SubscriptionController
                        sc.Subscription_Update(PortalId, ForumModuleId, ForumId, TopicId, 1, AuthorId, ForumUser.UserRoles)
                    End If
                ElseIf isEdit Then
                    Dim isSub As Boolean = Subscriptions.IsSubscribed(PortalId, ForumModuleId, ForumId, TopicId, SubscriptionTypes.Instant, AuthorId)
                    If isSub And Not ctlForm.Subscribe Then
                        Dim sc As New SubscriptionController
                        sc.Subscription_Update(PortalId, ForumModuleId, ForumId, TopicId, 1, AuthorId, ForumUser.UserRoles)
                    End If
                End If
                If bSend And Not isEdit Then
                    Subscriptions.SendSubscriptions(PortalId, ForumModuleId, ForumTabId, fi, TopicId, tmpReplyId, ri.Content.AuthorId)
                End If
                If ri.IsApproved = False Then
                    Dim oEmail As New Email
                    oEmail.SendEmailToModerators(ForumInfo.ModNotifyTemplateId, PortalId, ForumId, ri.TopicId, tmpReplyId, ForumModuleId, ForumTabId, String.Empty)
                    Dim Params() As String = {ParamKeys.ForumId & "=" & ForumId, ParamKeys.TopicId & "=" & TopicId, ParamKeys.ViewType & "=confirmaction", ParamKeys.ConfirmActionId & "=" & ConfirmActions.MessagePending}
                    Response.Redirect(Utilities.NavigateUrl(ForumTabId, "", Params), False)
                Else
                    Dim ctlUtils As New ControlUtils
                    Dim ti As TopicInfo = tc.Topics_Get(PortalId, ForumModuleId, TopicId, ForumId, -1, False)
                    Dim fullURL As String = ctlUtils.BuildUrl(ForumTabId, ForumModuleId, ForumInfo.ForumGroup.PrefixURL, ForumInfo.PrefixURL, ForumInfo.ForumGroupId, ForumInfo.ForumID, TopicId, ti.TopicUrl, -1, -1, String.Empty, 1, SocialGroupId)
                    If fullURL.Contains("~/") Or Not Request.QueryString("asg") Is Nothing Then
                        fullURL = Utilities.NavigateUrl(ForumTabId, "", New String() {ParamKeys.TopicId & "=" & TopicId, ParamKeys.ContentJumpId & "=" & tmpReplyId})
                    End If
                    If fullURL.EndsWith("/") Then
                        fullURL &= "?" & ParamKeys.ContentJumpId & "=" & tmpReplyId
                    End If

                    If Not isEdit Then
                        Try
                            Dim amas As New Social
                            amas.AddReplyToJournal(PortalId, ForumModuleId, ForumId, TopicId, ReplyId, UserId, fullURL, Subject, String.Empty, Body, ForumInfo.ActiveSocialSecurityOption, ForumInfo.Security.Read, SocialGroupId)
                            'If Request.QueryString("asg") Is Nothing And Not String.IsNullOrEmpty(MainSettings.ActiveSocialTopicsKey) And ForumInfo.ActiveSocialEnabled And Not ForumInfo.ActiveSocialTopicsOnly Then
                            '    amas.AddReplyToJournal(PortalId, ForumModuleId, ForumId, TopicId, ReplyId, UserId, fullURL, Subject, String.Empty, Body, ForumInfo.ActiveSocialSecurityOption, ForumInfo.Security.Read)
                            'Else
                            '    amas.AddForumItemToJournal(PortalId, ForumModuleId, UserId, "forumreply", fullURL, Subject, Body)
                            'End If


                        Catch ex As Exception
                            DotNetNuke.Services.Exceptions.LogException(ex)
                        End Try

                    End If
                    Response.Redirect(fullURL)
                End If
            Catch ex As Exception

            End Try
        End Sub
        Private Sub SaveAttach(ByVal ContentId As Integer)
            Dim AttachIds As String = hidAttachIds.Value
            Dim _fileManager As IFileManager = FileManager.Instance()



            If Not AttachIds = String.Empty Then
                For Each attachid As String In AttachIds.Split(CChar(";"))
                    If Not attachid.Trim = String.Empty Then
                        Dim tmpAttachId As Integer = CInt(attachid)
                        Dim _file As IFileInfo = _fileManager.GetFile(tmpAttachId)
                        Dim adb As New Data.AttachController
                        If _file Is Nothing Then
                            adb.SaveToContent(ContentId, tmpAttachId, Nothing, Nothing, False, Nothing)
                        Else
                            Dim fileUrl As String = "~/LinkClick.aspx?fileticket={0}"
                            Dim url As String = Page.ResolveUrl("~/LinkClick.aspx?fileid=" & _file.FileId)
                            fileUrl = String.Format(fileUrl, UrlUtils.EncryptParameter(UrlUtils.GetParameterValue(url)))
                            adb.SaveToContent(ContentId, tmpAttachId, fileUrl, _file.FileName, True, _file.ContentType)
                        End If

                    End If
                Next
            End If



        End Sub

#End Region

    End Class
End Namespace
